"""

This is a container object.  By definition, a container object has an "inside"
that objects can be put in.  The container may or may not be able to be opened
and closed.

Create this container with

 @create/drop <name>:container.Container

"""

from ev import Object
from game.gamesrc.commands import cmdset_container as cmdsetcontainer

#
# Definition of the object itself
#

class Container(Object):
    """
    This class describes a container object.  By definition, a container
    is an object that has an "inside" in which objects can be placed.

    The following attributes can be set on the button:
        self.db.has_lid -   Boolean describing whether the container has a lid that can be open/closed
        self.db.is_open -   Boolean describing whether the container is open or closed
        self.db.desc -      String description of the container object
    """
    def at_object_creation(self):
        """
        This function is called when object is created. Use this
        instead of e.g. __init__.
        """
                
        # Store the container states (has_lid, is_open)
        self.db.has_lid = True
        self.db.is_open = False

        # Call the description generator function
        self.gen_desc()
        
        # Add the default commandset for the container object
        self.cmdset.add_default(cmdsetcontainer.DefaultCmdSet, permanent=True)

    # state-changing methods

    def container_open(self):
        """
        Opens the container.
        """

        if self.db.is_open:
            return
        self.db.is_open = True
        self.gen_desc()
        
    def container_close(self):
        """
        Closes the container
        """

        if not self.db.is_open:
            return
        self.db.is_open = False
        self.gen_desc()
        
    def gen_desc(self):
        """
        Generates the object description. We put this in a method
        because the description is partially dynamic and describes
        whether the container is open or closed depending on state.
        """
        
        # Grab the object name and store desc (default, you can change this at creation time)
        container_name = self.name
        
        desc = "The %s is a container." % container_name
        
        if self.db.has_lid:
            desc += " It can be opened and closed."
            if self.db.is_open:
                desc += " It is currently open."
            else:
                desc += " It is currently closed."
        
        self.db.desc = desc